package com.xuetang9.fujian.socketdemo;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author: fujian
 * @description:
 * @date: 2021/5/9 10:53
 * @version: V1.0
 * @package: com.xuetang9.fujian.socketdemo
 **/
public class InetAddressDemo {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress address = null;
        // 获得本机的IP地址
        address = InetAddress.getLocalHost();
        System.out.println("主机名称：" + address.getHostName());
        System.out.println("主机地址：" + address.getHostAddress());
        byte[] bytesAddress = address.getAddress();
        for (int i = 0; i < bytesAddress.length; i++) {
            System.out.println(Byte.toUnsignedInt(bytesAddress[i]));
        }
        // 访问其他主机信息
        InetAddress[] addressesArr = InetAddress.getAllByName("www.xuetang9.com");
        for (int i = 0; i < addressesArr.length; i++) {
            System.out.println("主机名称：" + addressesArr[i].getHostName());
            System.out.println("主机地址：" + addressesArr[i].getHostAddress());
            byte[] bytes = addressesArr[i].getAddress();
            for (int j = 0; j < bytes.length; j++) {
                System.out.println(Byte.toUnsignedInt(bytes[j]));
            }
        }
    }
}
